//
// File: poincareHRV.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 23-Apr-2025 16:26:18
//

// Include Files
#include "poincareHRV.h"
#include "diff.h"
#include "freqDomainHRV_data.h"
#include "freqDomainHRV_initialize.h"
#include "freqDomainHRV_types.h"
#include "rt_nonfinite.h"
#include "std.h"
#include "coder_array.h"
#include <cmath>
#include <string.h>

// Function Definitions
//
// poincareHRV(ibi) - calculates poincare HRV
//
// Inputs:    ibi = 2dim array containing [t (s),ibi (s)]
//
// Outputs:   output is a structure containg HRV.
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//  Copyright (C) 2010, John T. Ramshur, jramshur@gmail.com
//
//  This file is part of HRVAS
//
//  HRVAS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  HRVAS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// Arguments    : coder::array<double, 2U> &ibi
//                struct7_T *output
// Return Type  : void
//
void poincareHRV(coder::array<double, 2U> &ibi, struct7_T *output)
{
  coder::array<double, 1U> c_ibi;
  coder::array<double, 1U> r;
  double a_tmp;
  int b_ibi;
  if (!isInitialized_freqDomainHRV) {
    freqDomainHRV_initialize();
  }
  // check inputs
  b_ibi = ibi.size(0) - 1;
  c_ibi.set_size(ibi.size(0));
  for (int i{0}; i <= b_ibi; i++) {
    c_ibi[i] = ibi[i + ibi.size(0)];
  }
  b_ibi = c_ibi.size(0);
  for (int i{0}; i < b_ibi; i++) {
    ibi[i + ibi.size(0)] = c_ibi[i];
  }
  // convert ibi to ms
  // assumes ibi units are seconds
  //      if abs(range(ibi(:,2)))<50 %assume ibi units are seconds
  //              ibi(:,2)=ibi(:,2).*1000; %convert ibi to ms
  //      end
  //      if abs(range(diff(ibi(:,1))))>50 %assume time unites are ms
  //          ibi(:,1)=ibi(:,1)./1000; %convert time to s
  //      end
  // successive differences
  b_ibi = ibi.size(0);
  c_ibi.set_size(ibi.size(0));
  for (int i{0}; i < b_ibi; i++) {
    c_ibi[i] = ibi[i + ibi.size(0)];
  }
  coder::diff(c_ibi, r);
  a_tmp = coder::b_std(r);
  b_ibi = ibi.size(0);
  c_ibi.set_size(ibi.size(0));
  for (int i{0}; i < b_ibi; i++) {
    c_ibi[i] = ibi[i + ibi.size(0)];
  }
  double a;
  a = coder::b_std(c_ibi);
  // format decimal places
  a_tmp = 0.5 * (a_tmp * a_tmp);
  output->SD1 = std::round(std::sqrt(a_tmp) * 10.0) / 10.0;
  // ms
  output->SD2 = std::round(std::sqrt(2.0 * (a * a) - a_tmp) * 10.0) / 10.0;
  // ms
}

//
// File trailer for poincareHRV.cpp
//
// [EOF]
//
